using System;
using System.Collections.Generic;
using System.Text;
using WindowsLive.Writer.Api;
using System.Windows.Forms;

namespace InsertIMDBInfo
{
    [WriterPlugin("7b08a82a-97b5-421f-9649-2ca5f2593cf8", "Insert IMDB Info",
        Description = "Insert information from IMDB.com",
        HasEditableOptions = false,
        Name = "Insert IMDB Info",
        ImagePath = "plugin.png",
        PublisherUrl = "http://scottisafooldev.spaces.live.com")]
    [InsertableContentSource("IMDB Info")]

    public class IMDBPlugin : SmartContentSource
    {
        PluginSettings m_default;
        public override void Initialize(IProperties pluginOptions)
        {
            base.Initialize(pluginOptions);
            m_default = new PluginSettings(pluginOptions);
        }

        public override DialogResult CreateContent(IWin32Window dialogOwner, ISmartContent newContent)
        {
            PluginSettings settings = new PluginSettings(newContent.Properties);
            settings.DefaultTemplate = m_default.DefaultTemplate;
            using (frmMain form = new frmMain(settings))
            {
                DialogResult result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    if (form.Thumbnail != null)
                    {
                        newContent.Files.AddImage(string.Format("{0}.jpg", settings.Title), form.Thumbnail, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                }
                return result;
            }
        }

        public override SmartContentEditor CreateEditor(ISmartContentEditorSite editorSite)
        {
            return new ContentEditor(m_default);
        }

        public override string GeneratePublishHtml(ISmartContent content, IPublishingContext publishingContext)
        {
            string HTMLCode = string.Empty;
            PluginSettings settings = new PluginSettings(content.Properties);
            bool[] options = settings.ParseTemplateOptions(settings.CustomTemplate);

            Alignment align = Alignment.Left;
            switch (settings.Alignment)
            {
                case 0:
                    align = Alignment.Left;
                    break;
                case 1:
                    align = Alignment.Right;
                    break;
                case 2:
                    align = Alignment.None;
                    break;
            }
            content.Layout.Alignment = align;

            // Generate the HTML Code
            if (!options[0] && !options[1] && !options[3])
                HTMLCode += "<div style=\"width: 155px; padding-left: 5px;\">\n";
            else
                HTMLCode += "<div style=\"width: 315px; padding-left: 5px;\">\n";
            
            HTMLCode += "<div id=\"left\" style=\"float: left;\">\n";

            if (options[2])
            {
                if ((content.Files != null) && (content.Files.Filenames != null) && (content.Files.Filenames.Length > 0))
                {
                    HTMLCode += string.Format("<img alt=\"{0}\" src=\"{1}\" />\n", settings.Title, content.Files.GetUri(content.Files.Filenames[0]));
                }
            }

            if (options[4])
                HTMLCode += string.Format("<br /><br />{0}\n", settings.Year);

            HTMLCode += string.Format("<br /><br /><a href=\"{0}\" target=\"_blank\">Go to IMDb page</a></p>\n", settings.IMDbLink);
            HTMLCode += "<p style=\"font-size:smaller\">Information &copy; <a href=\"http://www.imdb.com\" target=\"_blank\">IMDb.com</a></p>\n</div>";

            if (options[0] || options[1] || options[3])
            {
                HTMLCode += "<div style=\"float: right; width: 160; padding: 5px;\">\n";

                if (options[3])
                    HTMLCode += string.Format("<p>{0}</p>\n", settings.Title);

                if (options[0])
                    HTMLCode += string.Format("<p>{0}</p>\n", settings.Actors);

                if (options[1])
                    HTMLCode += string.Format("<p>{0}</p>\n", settings.Description);

                HTMLCode += "</div>\n";
            }
            
            HTMLCode += "</div>\n";

            return HTMLCode;
        }
    }
}
